package com.jinmingwu.musicplay.httpserver;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;

/**
 * Created by jinmingwu on 2018/10/24.
 */

public class LrcDownload {

    // 歌词API
    public static final String LRC_SEARCH_URL = "http://gecimi.com/api/lyric/";
    // 设置请求超时15秒
    public static final int REQUEST_TIMEOUT = 15 * 1000;
    // 设置等待超时15秒
    public static final int SO_TIMEOUT = 15*1000;

    public static String getSongLRCUrl (String path, String musicName) throws UnsupportedEncodingException, JSONException {
        String url = null;
        String str_json = null;

        // 歌名为空返回null
        if (musicName == null) {
            return null;
        }
        String name = URLEncoder.encode(musicName, "UTF-8");
        str_json = getHtmlCode(path + name);

        // 超时以及其他异常时返回null
        if (str_json == null) {
            return null;
        }
        JSONObject jsonObject = new JSONObject(str_json);
        int count = jsonObject.getInt("count");

        // 无歌词返回null
        if (count == 0){
            return null;
        }

        // 获取得到是歌词Url列表
        JSONArray jsonArray = jsonObject.getJSONArray("result");
        JSONObject item = jsonArray.getJSONObject(0);
        url = item.getString("lrc");
        return url;
    }

    public static HttpClient getHttpClient() {
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
        HttpClient client = new DefaultHttpClient(httpParams);
        return client;
    }

    // 获取网页源码
    public static String getHtmlCode(String path) {
        String result = null;
        HttpClient httpClient = getHttpClient();
        HttpGet get = new HttpGet(path);
        try {
            HttpResponse response = httpClient.execute(get);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
                String line;
                result = "";
                while ((line = br.readLine()) != null) {
                    result += line + "\n";
                }
            }
        } catch (ConnectTimeoutException e) {
            System.out.println("连接超时");
            e.printStackTrace();
        } catch (SocketTimeoutException e) {
            System.out.println("Socket连接超时");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println(e.getMessage() + ":" + e.toString());
            return null;
        }
        return result;
    }
}
